/**
 * 统一支付
 */

const api = require('../api/pay');

/**
 * 获取微信登录code
 */
const getCode = () => {
	return new Promise(function (success, fail) {
		wx.login({
			success(res) {
				if (res.code) {
					success(res.code)
				} else {
					fail('登录失败！' + res.errMsg);
				}
			}
		})
	})

};

//购买超级会员支付
const superUserOrder = (info) => {
	return new Promise(function (success, fail) {
		getCode().then(q => {
			let obj = Object.assign({ code: q }, info);

			api.createSuperUserOrder(obj).then(
				res => {
					let data = res.data.data || {};

					if (data.timeStamp) {
						wx.requestPayment({
							timeStamp: data.timeStamp + '',
							nonceStr: data.nonceStr,
							package: data.package,
							signType: data.signType,
							paySign: data.paySign,
							success(res) {
								success(res);
							},
							fail(err) {
								fail(err);
							}
						});
					} else {
						fail('返回数据非法');
					}
				}
			).catch(
				err => {
					fail(err);
				}
			);
		})
	})

};

//会员商城订单支付
const mallOrder = (obj) => {
	return new Promise(function (success, fail) {
		getCode().then(q => {
			obj.code = q;

			api.createPointsOrder(obj).then(
				res => {
					let data = res.data.data || {},
						wxresult = data.wxresult || {};

					if (data.canPay) {//微信支付
						if (wxresult.timeStamp) {
							wx.requestPayment({
								timeStamp: wxresult.timeStamp + '',
								nonceStr: wxresult.nonceStr,
								package: wxresult.package,
								signType: wxresult.signType,
								paySign: wxresult.paySign,
								success(wxres) {
									success(res);
								},
								fail(err) {
									fail(err);
								}
							});
						} else {
							fail('返回数据非法');
						}
					} else {
						success(res);
					}
				}
			).catch(
				err => {
					fail(err);
				}
			);
		})
	})

};

//预约金订单支付
const subscriptionOrder = (obj) => {
	return new Promise(function (success, fail) {
		getCode().then(q => {
			obj.code = q;

			api.createAppointmentPriceOrder(obj).then(
				res => {
					let data = res.data.data || {},
						wxresult = data.wxresult || {};

					if (data.canPay) {//微信支付
						if (wxresult.timeStamp) {
							wx.requestPayment({
								timeStamp: wxresult.timeStamp + '',
								nonceStr: wxresult.nonceStr,
								package: wxresult.package,
								signType: wxresult.signType,
								paySign: wxresult.paySign,
								success(wxres) {
									success(res);
								},
								fail(err) {
									fail(err);
								}
							});
						} else {
							fail('返回数据非法');
						}
					} else {
						success(res);
					}
				}
			).catch(
				err => {
					fail(err);
				}
			);
		})
	})

};

//sku订单支付
const skuOrder = (obj) => {
	return new Promise(async (success, fail) => {
		try {
			obj.code = await getCode();

			let res = await api.skuOrderPay(obj),
				data = res.data.data || {},
				content = data.content || {};

			if (content.timeStamp) {//微信支付
				wx.requestPayment({
					timeStamp: content.timeStamp + '',
					nonceStr: content.nonceStr,
					package: content.package,
					signType: content.signType,
					paySign: content.paySign,
					success(wxres) {
						success(res);
					},
					fail(err) {
						fail(err);
					}
				});
			} else {
				fail('返回数据非法');
			}
		} catch (e) {
			fail(e);
		}
	})
};

// 确认订单支付
const orderPay = (code) => {
	return new Promise(async (success, fail) => {
		try {
			// obj.code = await getCode();

			let res = await api.wxOrderpay(code),
			content = res.data.ResData || {};

			if (content.timeStamp) {//微信支付
				wx.requestPayment({
					timeStamp: content.timeStamp + '',
					nonceStr: content.nonceStr,
					package: 'prepay_id=' +content.prepay_id,
					paySign: content.paySign,
					signType: 'MD5',
					success(res) {
						success(res);
					},
					fail(err) {
						fail(err);
					}
				});
			} else {
				fail('返回数据非法');
			}
		} catch (e) {
			fail(e);
		}
	})
};

module.exports = {
	superUserOrder,
	mallOrder,
	skuOrder,
	subscriptionOrder,
	orderPay
}